package com.stackexchange.webapis.methodgroups
{
	import com.stackexchange.webapis.events.StackExchangeResultEvent;
	
	import flash.events.Event;
	import flash.net.URLLoader;
	import com.stackexchange.webapis.client.StackExchangeService;

	/**
	 * Broadcast as a result of the getAll method being called
	 *
	 * The event contains the following properties
	 *	success	- Boolean indicating if the call was successful or not
	 *	data - When success is true, contains an "badges" BadgeList instance
	 *		   When success is false, contains an "error" StackExchangeError instance
	 *
	 * @see com.producteev.webapis.StackExchangeError
	 * @see com.producteev.webapis.BadgeList
	 */
	[Event(name="badgesGetAll", type="com.stackexchange.webapis.events.StackExchangeResultEvent")]
	
	/**
	 * Broadcast as a result of the getUsersByBadgeId method being called
	 *
	 * The event contains the following properties
	 *	success	- Boolean indicating if the call was successful or not
	 *	data - When success is true, contains an "badges" UserList instance
	 *		   When success is false, contains an "error" StackExchangeError instance
	 *
	 * @see com.producteev.webapis.StackExchangeError
	 * @see com.producteev.webapis.UserList
	 */
	[Event(name="badgesGetByUser", type="com.stackexchange.webapis.events.StackExchangeResultEvent")]
	
	/**
	 * Broadcast as a result of the getAllName method being called
	 *
	 * The event contains the following properties
	 *	success	- Boolean indicating if the call was successful or not
	 *	data - When success is true, contains an "badges" BadgeList instance
	 *		   When success is false, contains an "error" StackExchangeError instance
	 *
	 * @see com.producteev.webapis.StackExchangeError
	 * @see com.producteev.webapis.BadgeList
	 */
	[Event(name="badgesGetAllName", type="com.stackexchange.webapis.events.StackExchangeResultEvent")]
	
	/**
	 * Broadcast as a result of the getAllTags method being called
	 *
	 * The event contains the following properties
	 *	success	- Boolean indicating if the call was successful or not
	 *	data - When success is true, contains an "badges" BadgeList instance
	 *		   When success is false, contains an "error" StackExchangeError instance
	 *
	 * @see com.producteev.webapis.StackExchangeError
	 * @see com.producteev.webapis.BadgeList
	 */
	[Event(name="badgesGetAllTags", type="com.stackexchange.webapis.events.StackExchangeResultEvent")]
	
	public class Badges extends AbstractMethod
	{
		private static const GET:String = "";
		private static const GET_USERS:String = "{id}";
		private static const GET_NAME:String = "name";
		private static const GET_TAGS:String = "tags";
		
		public function Badges(service:StackExchangeService, methodCaller:MethodCaller, responseParser:ResponseParser)
		{
			super(service, methodCaller, responseParser);
		}
		
		/**
		 * Gets all badges, in alphabetical order.   
		 *
		 * @param params a list of params
		 * 
		 * @see http://api.stackoverflow.com/1.0/help/method?method=badges
		 */
		public function getAll(params:Object=null):void
		{
			call(GET, getAllHandler, null, params);
		}
		
		private function getAllHandler(event:Event):void
		{
			processAndDispatch(URLLoader(event.target).data,
				StackExchangeResultEvent.BADGES_GET_ALL,
				_responseParser.parseBadges);
		}
		
		/**
		 * Gets the users that have been awarded the badges identified in 'id'.
		 * 
		 * @param badgeIds A single primary key identifier or an array of identifiers
		 * 
		 * @see http://api.stackoverflow.com/1.0/help/method?method=badges/{id}
		 * 
		 **/
		public function getUsersByBadgeId(badgeIds:Array, params:Object=null):void
		{
			call(GET_USERS, getUsersByBadgeIdHandler, {id:badgeIds}, params);
		}
		
		private function getUsersByBadgeIdHandler(event:Event):void
		{
			processAndDispatch(URLLoader(event.target).data,
				StackExchangeResultEvent.BADGES_GET_BY_USER,
				_responseParser.parseUsers);
		}
		
		/**
		 * Gets all standard, non-tag-based badges in alphabetical order.    
		 *
		 * @param params a list of params
		 * 
		 * @see http://api.serverfault.com/1.0/help/method?method=badges/name
		 */
		public function getAllName(params:Object=null):void
		{
			call(GET_NAME, getAllNameHandler, null, params);
		}
		
		private function getAllNameHandler(event:Event):void
		{
			processAndDispatch(URLLoader(event.target).data,
				StackExchangeResultEvent.BADGES_GET_ALL_NAME,
				_responseParser.parseBadges);
		}
		
		/**
		 * Gets all tag-based badges in alphabetical order.    
		 *
		 * @param params a list of params
		 * 
		 * @see http://api.serverfault.com/1.0/help/method?method=badges/tags
		 */
		public function getAllTags(params:Object=null):void
		{
			call(GET_TAGS, getAllTagsHandler, null, params);
		}
		
		private function getAllTagsHandler(event:Event):void
		{
			processAndDispatch(URLLoader(event.target).data,
				StackExchangeResultEvent.BADGES_GET_All_TAGS,
				_responseParser.parseBadges);
		}
	}
}